/**
 * 用户管理API接口
 */

import { request } from '@/utils/request'
import type {
  ApiResponse,
  User,
  UserStatus,
  UserCreateRequest,
  UserUpdateRequest,
  UserListQuery,
  UserListResponse,
  UserDetailResponse
} from '@/types/rbac'

// API路径前缀
const API_PREFIX = '/api/v1'

/**
 * 用户管理API
 */
export const userApi = {
  /**
   * 获取用户列表
   */
  async getUsers(params?: UserListQuery): Promise<UserListResponse> {
    return request.get(`${API_PREFIX}/users`, { params })
  },

  /**
   * 获取用户详情
   */
  async getUserById(id: number): Promise<UserDetailResponse> {
    return request.get(`${API_PREFIX}/users/${id}`)
  },

  /**
   * 创建用户
   */
  async createUser(data: UserCreateRequest): Promise<UserDetailResponse> {
    return request.post(`${API_PREFIX}/users`, data)
  },

  /**
   * 更新用户
   */
  async updateUser(id: number, data: UserUpdateRequest): Promise<UserDetailResponse> {
    return request.put(`${API_PREFIX}/users/${id}`, data)
  },

  /**
   * 删除用户
   */
  async deleteUser(id: number, hardDelete = false): Promise<ApiResponse> {
    return request.delete(`${API_PREFIX}/users/${id}`, {
      params: { hard_delete: hardDelete }
    })
  },

  /**
   * 激活用户
   */
  async activateUser(id: number): Promise<ApiResponse> {
    return request.post(`${API_PREFIX}/users/${id}/activate`)
  },

  /**
   * 暂停用户
   */
  async suspendUser(id: number, reason: string): Promise<ApiResponse> {
    return request.post(`${API_PREFIX}/users/${id}/suspend`, null, {
      params: { reason }
    })
  },

  /**
   * 重置用户密码
   */
  async resetUserPassword(id: number, data: {
    new_password: string
    send_email?: boolean
  }): Promise<ApiResponse> {
    return request.post(`${API_PREFIX}/users/${id}/reset-password`, data)
  },

  /**
   * 获取用户统计信息
   */
  async getUserStatistics(): Promise<ApiResponse<{
    user_counts: {
      total: number
      active: number
      inactive: number
      suspended: number
      locked: number
      recent_active: number
    }
    department_distribution: Array<{
      department: string
      count: number
    }>
    role_distribution: Array<{
      role: string
      count: number
    }>
  }>> {
    return request.get(`${API_PREFIX}/users/statistics`)
  },

  /**
   * 批量更新用户
   */
  async batchUpdateUsers(data: {
    user_ids: number[]
    updates: Record<string, any>
  }): Promise<ApiResponse> {
    return request.post(`${API_PREFIX}/users/batch-update`, data)
  },

  /**
   * 导出用户数据
   */
  async exportUsers(params?: {
    format?: 'csv' | 'excel'
    filters?: UserListQuery
  }): Promise<Blob> {
    const response = await request.get(`${API_PREFIX}/users/export`, {
      params,
      responseType: 'blob'
    })
    return response as unknown as Blob
  },

  /**
   * 导入用户数据
   */
  async importUsers(file: File): Promise<ApiResponse<{
    success_count: number
    error_count: number
    errors: Array<{
      row: number
      message: string
    }>
  }>> {
    return request.upload(`${API_PREFIX}/users/import`, file)
  },

  /**
   * 检查用户名可用性
   */
  async checkUsernameAvailability(username: string): Promise<ApiResponse<{
    available: boolean
  }>> {
    return request.get(`${API_PREFIX}/users/check-username`, {
      params: { username }
    })
  },

  /**
   * 检查邮箱可用性
   */
  async checkEmailAvailability(email: string): Promise<ApiResponse<{
    available: boolean
  }>> {
    return request.get(`${API_PREFIX}/users/check-email`, {
      params: { email }
    })
  },

  /**
   * 获取用户活动日志
   */
  async getUserActivityLog(id: number, params?: {
    page?: number
    size?: number
    start_date?: string
    end_date?: string
  }): Promise<ApiResponse<Array<{
    id: number
    action: string
    details: string
    ip_address: string
    user_agent: string
    created_at: string
  }>>> {
    return request.get(`${API_PREFIX}/users/${id}/activity`, { params })
  },

  /**
   * 获取用户登录历史
   */
  async getUserLoginHistory(id: number, params?: {
    page?: number
    size?: number
  }): Promise<ApiResponse<Array<{
    id: number
    ip_address: string
    user_agent: string
    login_time: string
    logout_time?: string
    status: 'success' | 'failed'
  }>>> {
    return request.get(`${API_PREFIX}/users/${id}/login-history`, { params })
  }
}

export default userApi